package org.darkforest.common.services.impl;

import org.apache.commons.lang.StringUtils;
import org.darkforest.common.services.CartService;
import org.darkforest.dao.mapper.CartMapper;
import org.darkforest.model.Cart;
import org.darkforest.model.Member;
import org.darkforest.vo.CartAjaxAmountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Libing on 2015/3/23.
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    public List<Cart> findCartList(String userId, String selected, Integer storeId) {
        Cart param = new Cart();
        param.setBuyerId(userId);
        param.setSelected(selected);
        param.setStoreId(storeId);
        List<Cart> carts = cartMapper.findCartGoodsByCondition(param);
        return carts;
    }

    @Override
    public void addCart(Cart cart) {
        cartMapper.insert(cart);
    }

    @Override
    public CartAjaxAmountVo getCartTotalInfo(String userId,String selected) {
        CartAjaxAmountVo res = new CartAjaxAmountVo();
        Cart param = new Cart();
        param.setBuyerId(userId);
        param.setSelected(selected);
        param = cartMapper.getCartTotal(param);
//        res.setCartQty();
        res.setSubtotal(param.getGoodsPrice());
        res.setQty(param.getGoodsNum());
        return res;
    }

    @Override
    public void updateCartSelected(Map param) {
        cartMapper.updateCartSelected(param);
    }

    @Override
    public void updateUserIdAfterLogin(Cart cart) {
        cartMapper.updateUserIdAfterLogin(cart);
    }

    /**
     * 根据用户编号，商品编号获取已有购物车信息
     * @param userId
     * @param goodsId
     * @return
     */
    @Override
    public Cart getCartInfo(String userId, Integer goodsId) {
        Cart cart = new Cart();
        cart.setBuyerId(userId);
        cart.setGoodsId(goodsId);
        List<Cart> res = cartMapper.findCartGoodsByCondition(cart);
        return (res==null||res.size()==0)?null:res.get(0);
    }

    @Override
    public void updateCartGoodsNum(Cart cart) {
        cartMapper.updateByPrimaryKeySelective(cart);
    }

    @Override
    public void clearCartByBuyerId(String userId) {
        cartMapper.deleteByBuyerId(userId);
    }

    @Override
    public void deleteById(Integer cartId) {
        cartMapper.deleteByPrimaryKey(cartId);
    }

    @Override
    public List<Cart> getCartStoreInfo(String userId) {
        return cartMapper.getCartStoreInfo(userId);
    }

    @Override
    public List<Integer> findStoreByUserBuyGoods(Integer userId) {
        return cartMapper.findStoreByUserBuyGoods(userId);
    }

    /**
     * 根据用户ID 以及购物车ID批量查询购物车内容
     *
     * @param userId
     * @param cardIds
     * @return
     */
    @Override
    public List<Cart> selectCartListByIds(Integer userId, String cardIds) {
        if (StringUtils.isNotBlank(cardIds)){
            Map<String,Object> condition = new HashMap<String, Object>();
            condition.put("userId",userId);
            condition.put("cartIds",cardIds.split(","));
            return cartMapper.selectCartListByIds(condition);
        }else{
            return new ArrayList<Cart>();
        }

    }

    /**
     * 根据商品公共ID 删除购物车商品
     *
     * @param goodsCommonIds
     */
    @Override
    public void deleteByGoodsCommonId(String[] goodsCommonIds) {
        cartMapper.deleteByGoodsCommonids(goodsCommonIds);
    }

    @Override
    public int deleteBatch(Member member, String cartIds) {
        Map map = new HashMap();
        map.put("buyer_id",member.getMemberId());
        map.put("list",cartIds.split(","));
        return cartMapper.deleteBatch(map);
    }
}
